package com.app.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.app.common.RabbitConst.DELAY_EXCHANGE;
import static com.app.common.RabbitConst.DELAY_ROUTING_KEY;

/**
 * @author xiaoyu.fang
 * @date 2020/1/8 16:13
 */
@Slf4j
@Service
public class RabbitSender {

    // 十秒
    private static final String DELAY_TIME = "10000";

    @Autowired
    private AmqpTemplate rabbitTemplate;

    /**
     * 发送延迟消息
     *
     * @param msg
     */
    public void sendDelayMsg(String msg) {
        try {
            log.info("{} 开始发送消息：{}", new Date(), msg);
            rabbitTemplate.convertAndSend(DELAY_EXCHANGE, DELAY_ROUTING_KEY, msg, message -> {
                message.getMessageProperties().setExpiration(DELAY_TIME);
                return message;
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
